//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CPIVolatilitySurface.h"
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (boost::shared_ptr<QuantLib::CPIVolatilitySurface>& childNative, Object^ owner) : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
#ifdef HANDLE
	_phCPIVolatilitySurface = NULL;
#endif
	_ppCPIVolatilitySurface = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
}
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (QuantLib::CPIVolatilitySurface& childNative, Object^ owner) : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
#ifdef HANDLE
	_phCPIVolatilitySurface = NULL;
#endif
	_ppCPIVolatilitySurface = new boost::shared_ptr<QuantLib::CPIVolatilitySurface> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
    _CPIVolatilitySurfaceOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (CCPIVolatilitySurface^ copy) : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
#ifdef HANDLE
	_phCPIVolatilitySurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCPIVolatilitySurface = new boost::shared_ptr<QuantLib::CPIVolatilitySurface> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (PLATFORM::Type^ t) : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
#ifdef HANDLE
	_phCPIVolatilitySurface = NULL;
#endif
	if (!t->IsSubclassOf(CCPIVolatilitySurface::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (QuantLib::Handle<QuantLib::CPIVolatilitySurface>& childNative, Object^ owner)  : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
	_phCPIVolatilitySurface = &childNative;
	_ppCPIVolatilitySurface = &static_cast<boost::shared_ptr<QuantLib::CPIVolatilitySurface>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
    _CPIVolatilitySurfaceOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (QuantLib::Handle<QuantLib::CPIVolatilitySurface> childNative)  : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
	_phCPIVolatilitySurface = &childNative;
	_ppCPIVolatilitySurface = &static_cast<boost::shared_ptr<QuantLib::CPIVolatilitySurface>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::CCPIVolatilitySurface (QuantLib::CPIVolatilitySurface childNative)  : CVolatilityTermStructure(CCPIVolatilitySurface::typeid)
{
#ifdef HANDLE
	_phCPIVolatilitySurface = NULL;
#endif
	_ppCPIVolatilitySurface = new boost::shared_ptr<QuantLib::CPIVolatilitySurface> (new QuantLib::CPIVolatilitySurface (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppCPIVolatilitySurface));
}
#endif

Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::~CCPIVolatilitySurface ()
{
    if (_ppCPIVolatilitySurface != NULL)
    {
	    delete _ppCPIVolatilitySurface;
        _ppCPIVolatilitySurface = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::!CCPIVolatilitySurface ()
{
    if (_ppCPIVolatilitySurface != NULL)
    {
	    delete _ppCPIVolatilitySurface;
    }
}
QuantLib::CPIVolatilitySurface& Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::GetReference ()
{
    if (_ppCPIVolatilitySurface == NULL) throw REFNEW NativeNullException ();
	return **_ppCPIVolatilitySurface;
}
boost::shared_ptr<QuantLib::CPIVolatilitySurface>& Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::GetShared ()
{
    if (_ppCPIVolatilitySurface == NULL) throw REFNEW NativeNullException ();
	return *_ppCPIVolatilitySurface;
}
QuantLib::CPIVolatilitySurface* Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::GetPointer ()
{
    if (_ppCPIVolatilitySurface == NULL) throw REFNEW NativeNullException ();
	return &**_ppCPIVolatilitySurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CPIVolatilitySurface>& Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::GetHandle ()
{
	if (_phCPIVolatilitySurface == NULL)
	{
		_phCPIVolatilitySurface = new Handle<QuantLib::CPIVolatilitySurface> (*_ppCPIVolatilitySurface);
	}
	return *_phCPIVolatilitySurface;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::HasNative () 
{
	return (_ppCPIVolatilitySurface != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::BaseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCPIVolatilitySurface)->baseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::BaseLevel::get ()
{
    try
    {
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCPIVolatilitySurface)->baseLevel ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::FrequencyEnum Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppCPIVolatilitySurface)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::IndexIsInterpolated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCPIVolatilitySurface)->indexIsInterpolated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPIVolatilitySurface)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPIVolatilitySurface)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::ObservationLag::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppCPIVolatilitySurface)->observationLag ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::TimeFromBase (DateTime date, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ obsLag)
{
    CPeriod^ _CobsLag;
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag))
        {
            _CobsLag = safe_cast<CPeriod^> (obsLag->Value);
            _CobsLag->Lock();
        }
        QuantLib::Period& _obsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag) ? static_cast<QuantLib::Period&> (_CobsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppCPIVolatilitySurface)->timeFromBase ( _date,  _obsLag );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CobsLag != nullptr) _CobsLag->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::TotalVariance (Cephei::QL::Times::IPeriod^ optionTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ obsLag, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CobsLag;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag))
        {
            _CobsLag = safe_cast<CPeriod^> (obsLag->Value);
            _CobsLag->Lock();
        }
        QuantLib::Period& _obsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag) ? static_cast<QuantLib::Period&> (_CobsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCPIVolatilitySurface)->totalVariance ( _optionTenor,  _strike,  _obsLag,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CobsLag != nullptr) _CobsLag->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::TotalVariance (DateTime exerciseDate, Double strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ obsLag, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CobsLag;
    try
    {
        QuantLib::Date _exerciseDate = (QuantLib::Date)ValueHelper::Convert (exerciseDate); //a
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag))
        {
            _CobsLag = safe_cast<CPeriod^> (obsLag->Value);
            _CobsLag->Lock();
        }
        QuantLib::Period& _obsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag) ? static_cast<QuantLib::Period&> (_CobsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCPIVolatilitySurface)->totalVariance ( _exerciseDate,  _strike,  _obsLag,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CobsLag != nullptr) _CobsLag->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::Volatility (Cephei::QL::Times::IPeriod^ optionTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ obsLag, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CobsLag;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag))
        {
            _CobsLag = safe_cast<CPeriod^> (obsLag->Value);
            _CobsLag->Lock();
        }
        QuantLib::Period& _obsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag) ? static_cast<QuantLib::Period&> (_CobsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCPIVolatilitySurface)->volatility ( _optionTenor,  _strike,  _obsLag,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CobsLag != nullptr) _CobsLag->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Inflation::CCPIVolatilitySurface::Volatility (DateTime maturityDate, Double strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ obsLag, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CobsLag;
    try
    {
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //a
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag))
        {
            _CobsLag = safe_cast<CPeriod^> (obsLag->Value);
            _CobsLag->Lock();
        }
        QuantLib::Period& _obsLag = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (obsLag) ? static_cast<QuantLib::Period&> (_CobsLag->GetReference ()) : QuantLib::Period(-1,Days)); //6
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCPIVolatilitySurface)->volatility ( _maturityDate,  _strike,  _obsLag,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CobsLag != nullptr) _CobsLag->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

